package com.wsc58888.it.wx.service;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.java.st.lib.tools.bean.EntityToUrlParamUtil;
import com.wsc58888.it.wx.stock.bean.StockBudgetRequst;
import com.wsc58888.it.wx.stock.bean.StockBudgetResponse;
import com.wsc58888.it.wx.stock.bean.StockCouponCodesRequest;
import com.wsc58888.it.wx.stock.bean.StockCouponCodesResponse;
import com.wsc58888.it.wx.stock.bean.StockCreateRequest;
import com.wsc58888.it.wx.stock.bean.StockCreateResponse;
import com.wsc58888.it.wx.stock.bean.StockDetailsResponse;
import com.wsc58888.it.wx.stock.bean.StockNotifyRequest;
import com.wsc58888.it.wx.stock.bean.StockNotifyResponse;
import com.wsc58888.it.wx.stock.bean.StockUpdateRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsAssociateOrderRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsAssociateOrderResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsDeactivateRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsDeactivateResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsDisassociateOrderResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsPageResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsReturnRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsReturnResponse;
import com.wsc58888.it.wx.stock.bean.UsersCouponsWriteOffRequest;
import com.wsc58888.it.wx.stock.bean.UsersCouponsWriteOffResponse;
import com.wsc58888.it.wx.stock.bean.WXImageUploadResponse;
import com.wsc58888.it.wx.wechat.WechatPayConfig;
import com.wsc58888.it.wx.wechat.WechatPayHttpClientUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WxStocksInterfaceInvoker {

	/**
	 * 创建商家券
	 */
	public static StockCreateResponse createStocks(StockCreateRequest stockCreateRequest) {
		String responseStr = null;
		try {
			responseStr =WechatPayHttpClientUtil.postStream(WechatPayConfig.createStocksURL,
					JSON.toJSONString(stockCreateRequest));
			log.info("创建商家券 Request \r\n {} Response \r\n {}",	JSON.toJSONString(stockCreateRequest),JSON.toJSONString(responseStr));
		} catch (IOException e) {
			log.error("请求时IO异常,地址：{}，参数：{},error:{}",  WechatPayConfig.createStocksURL,JSON.toJSONString(stockCreateRequest), e);
		}
		StockCreateResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockCreateResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 查询商家券详情
	 */
	public static StockDetailsResponse findStocks(String stock_id) {
		String responseStr = null;
		try {
			String url = String.format(WechatPayConfig.findStocksURL, stock_id);
			responseStr = WechatPayHttpClientUtil.doGet(url);
		} catch (IOException e) {
			log.error("请求时IO异常,地址：{}，参数：{},error:{}",  WechatPayConfig.createStocksURL,stock_id, e);
		}
		StockDetailsResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockDetailsResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 核销用户券
	 * @param usersCouponsWriteOffRequest 
	 * @return 
	 */
	public static UsersCouponsWriteOffResponse usersCouponsWriteOff(UsersCouponsWriteOffRequest usersCouponsWriteOffRequest) {
		String responseStr = null;
		try {
			responseStr = WechatPayHttpClientUtil.postStream(WechatPayConfig.usersCouponsWriteOffURL,
					JSON.toJSONString(usersCouponsWriteOffRequest));
		} catch (IOException e) {
			log.error("请求时IO异常,地址：{}，参数：{},error:{}",  WechatPayConfig.usersCouponsWriteOffURL,JSON.toJSONString(usersCouponsWriteOffRequest), e);
		}
		UsersCouponsWriteOffResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsWriteOffResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 根据过滤条件查询用户券
	 * @return 
	 */
	public static UsersCouponsPageResponse findUsersCouponsPage(String openid ,UsersCouponsRequest usersCouponsRequest) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.findUsersCouponsPageURL, openid);
			String param = EntityToUrlParamUtil.entityToUrlParam(usersCouponsRequest);
			String getUrl = url+param;
			log.info("根据过滤条件查询用户券:requst {}" , getUrl);
			responseStr = WechatPayHttpClientUtil.doGet(getUrl);
		} catch (IOException e) {
			log.error("请求时IO异常,地址：{}，参数：{},error:{}",  WechatPayConfig.findUsersCouponsPageURL,JSON.toJSONString(usersCouponsRequest), e);
		}
		UsersCouponsPageResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsPageResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 查询用户单张券详情API
	 * 
	 * @param 券code  coupon_code
	 * @param 公众账号ID appid
	 * @param 用户标识   openid
	 * @return 
	 * 
	 */
	public static UsersCouponsResponse findUsersCouponsDetail(String coupon_code, String appid, String openid) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.findUsersCouponsDetailURL,coupon_code,appid, openid);
			log.info(" 查询用户单张券详情API:requst {}" , url);
			responseStr = WechatPayHttpClientUtil.doGet(url);
		} catch (IOException e) {
			log.error("请求时IO异常，参数：coupon_code: {},appid: {},openid: {},,error:{}",coupon_code,appid,openid, e);
		}
		UsersCouponsResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 上传预存code
	 * @return 
	 */
	public static StockCouponCodesResponse uploadCouponCodes(String stock_id,StockCouponCodesRequest stockCouponCodesRequest) {
		
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.uploadCouponCodesURL,stock_id);
			log.info(" 上传预存code :request {}" ,JSON.toJSONString(stockCouponCodesRequest));
			responseStr = WechatPayHttpClientUtil.postStream(url, JSON.toJSONString(stockCouponCodesRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(stockCouponCodesRequest), e);
		}
		StockCouponCodesResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockCouponCodesResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 设置商家券事件通知地址API
	 * @param stockNotifyRequest 
	 * 
	 */
	public static StockNotifyResponse updateNotifyUrl(StockNotifyRequest stockNotifyRequest) {
		String responseStr = null;
		try {
			responseStr = WechatPayHttpClientUtil.postStream(WechatPayConfig.updateNotifyUrlURL, JSON.toJSONString(stockNotifyRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(stockNotifyRequest), e);
		}
		StockNotifyResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockNotifyResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 查询商家券事件通知地址API
	 * @param mchid 
	 * @return 
	 */
	public static  StockNotifyResponse findNotifyUrl(String mchid) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.findNotifyUrlURL);
			String param =  "";
			if(!StringUtils.isEmpty(mchid)) {
				param="?mchid="+mchid;
			}
			String getUrl = url+param;
			log.info(" 查询商家券事件通知地址 :request {}" ,getUrl);
			responseStr = WechatPayHttpClientUtil.doGet(getUrl);
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",mchid, e);
		}
		StockNotifyResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockNotifyResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 关联订单信息API
	 * @param usersCouponsAssociateOrderRequest 
	 * @return 
	 */
	public static UsersCouponsAssociateOrderResponse associateOrder(UsersCouponsAssociateOrderRequest usersCouponsAssociateOrderRequest) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.associateOrderURL);
			log.info(" 关联订单信息API :request {}" ,JSON.toJSONString(usersCouponsAssociateOrderRequest));
			responseStr = WechatPayHttpClientUtil.postStream(url, JSON.toJSONString(usersCouponsAssociateOrderRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(usersCouponsAssociateOrderRequest), e);
		}
		UsersCouponsAssociateOrderResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsAssociateOrderResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 取消关联订单信息API
	 * @param usersCouponsAssociateOrderRequest 
	 * @return 
	 */
	public static UsersCouponsDisassociateOrderResponse disassociateOrder(UsersCouponsAssociateOrderRequest usersCouponsAssociateOrderRequest) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.disassociateOrderURL);
			log.info(" 取消关联订单信息API:request {}" ,JSON.toJSONString(usersCouponsAssociateOrderRequest));
			responseStr = WechatPayHttpClientUtil.postStream(url, JSON.toJSONString(usersCouponsAssociateOrderRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(usersCouponsAssociateOrderRequest), e);
		}
		UsersCouponsDisassociateOrderResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsDisassociateOrderResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 修改批次预算API OK
	 * @param stock_id 
	 * @param stockBudgetRequst 
	 * @return 
	 */
	public static StockBudgetResponse updateBudget(String stock_id, StockBudgetRequst stockBudgetRequst) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.updateBudgetURL,stock_id);
			log.info(" 修改批次预算API request {}" ,JSON.toJSONString(stockBudgetRequst));
			responseStr = WechatPayHttpClientUtil.patchStream(url, JSON.toJSONString(stockBudgetRequst));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(stockBudgetRequst), e);
		}
		StockBudgetResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<StockBudgetResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 修改商家券基本信息API
	 * @param stockUpdateRequest 
	 */
	public static void updateStocks(StockUpdateRequest stockUpdateRequest,String stock_id) {
		try {
			String url = String.format(WechatPayConfig.updateStocksURL, stock_id);
			log.info("更新商家券 rurl {} \r\n Request \r\n {}  ",url,	JSON.toJSONString(stockUpdateRequest));
			 WechatPayHttpClientUtil.patchStream(url,JSON.toJSONString(stockUpdateRequest));
		} catch (IOException e) {
			log.error("请求时IO异常,地址：{}，参数：{},error:{}",  WechatPayConfig.createStocksURL,JSON.toJSONString(stockUpdateRequest), e);
		}
	}

	/**
	 * 申请退券API
	 * @param usersCouponsReturnRequest 
	 * @return 
	 */
	public static UsersCouponsReturnResponse returnCoupons(UsersCouponsReturnRequest usersCouponsReturnRequest) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.returnCouponsURL);
			responseStr = WechatPayHttpClientUtil.postStream(url, JSON.toJSONString(usersCouponsReturnRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(usersCouponsReturnRequest), e);
		}
		UsersCouponsReturnResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsReturnResponse>() {
		});
		return responseDTO;
	}

	/**
	 * 使券失效API
	 * @param usersCouponsDeactivateRequest 
	 * @return 
	 */
	public static UsersCouponsDeactivateResponse deactivateCoupons(UsersCouponsDeactivateRequest usersCouponsDeactivateRequest) {
		String responseStr = null;
		try {
			String url =String.format(WechatPayConfig.deactivateCouponsURL);
			log.info("使券失效API request {}" ,JSON.toJSONString(usersCouponsDeactivateRequest));
			responseStr = WechatPayHttpClientUtil.postStream(url, JSON.toJSONString(usersCouponsDeactivateRequest));
		} catch (IOException e) {
			log.error("请求时IO异常，参数：request: {},error:{}",JSON.toJSONString(usersCouponsDeactivateRequest), e);
		}
		UsersCouponsDeactivateResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<UsersCouponsDeactivateResponse>() {
		});
		return responseDTO;
		
	}
	/**
	 * 图片上传（营销）API
	 * @param usersCouponsDeactivateRequest 
	 * @return 
	 */
	public static WXImageUploadResponse uploadImage(String  filePath) {
		String responseStr = null;

		try {
			String url =String.format(WechatPayConfig.imageUploadURL);
			responseStr = WechatPayHttpClientUtil.uploadImage(url, filePath);
		} catch (Exception e) {
			log.error("请求时IO异常，参数：   {},error:{}", e);
		}
		WXImageUploadResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<WXImageUploadResponse>() {
		});
		return responseDTO;
	}

	public static WXImageUploadResponse uploadImage(FileInputStream fileInputStream) {
		String responseStr = null;

		try {
			String url =String.format(WechatPayConfig.imageUploadURL);
			responseStr = WechatPayHttpClientUtil.uploadImage(url, fileInputStream);
		} catch (Exception e) {
			log.error("请求时IO异常，参数：   {},error:{}", e);
		}
		WXImageUploadResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<WXImageUploadResponse>() {
		});
		return responseDTO;
	}

	 

	public static WXImageUploadResponse uploadImage(MultipartFile file) {
		String responseStr = null;

		try {
			String url =String.format(WechatPayConfig.imageUploadURL);
			responseStr = WechatPayHttpClientUtil.uploadImage(url, file);
		} catch (Exception e) {
			log.error("请求时IO异常，参数：   {},error:{}", e);
		}
		WXImageUploadResponse responseDTO = JSON.parseObject(responseStr, new TypeReference<WXImageUploadResponse>() {
		});
		return responseDTO;
	}

}
